# coding:utf-8
import pandas as pd

chengji = [
    [100, 95, 100, 98], [90, 98, 99, 94], [88, 95, 98, 95], [99, 98, 97, 92],
    [95, 90, 96, 88], [94, 94, 93, 77]
]
data = pd.DataFrame(chengji, columns=['语文', '类别', '数学', '政治'])

data4 = data.copy(deep=True)
data4 = data4[['语文', '数学']]
data5 = data4[data4['数学'] >= 95]
print("*" * 50, "筛选数学大于95")
print(data5)

data4 = data.copy(deep=True)
data5 = data4[~(data4['政治'] >= 95)]  # ~取反，要注意（）
print("*" * 50, "筛选政治小于95")
print(data5)

data4 = data.copy(deep=True)
data4.loc[1] = "优" + data4.loc[1].astype(str)  # ~取反，要注意（）
print("*" * 50, "行前加字符")
print(data4)
data5 = data4[['语文', '数学']][data4['数学'].str[0:1] == ("优")]  # .astype(str)
# 全部采用时，可去掉前的面[['语文','数学']]
print("*" * 50, "筛选出行前有优字符")
print(data5)
data5 = data4[data4['数学'].astype(str).str.contains("优")]
print("*" * 50, "筛选出行前有优字符")
print(data5)

data4 = data.copy(deep=True)
data4['政治'] = "优" + data4['政治'].astype(str)  # str,'str'都可以
print("*" * 50, "列前加字符")
print(data4)
data5 = data4.loc[:, data4.loc[1].astype(str).str.contains("优")]
print("*" * 50, "筛选出列前有优字符")
print(data5)
data5 = data4.loc[:, data.columns.str.contains('语|数')]
print("*" * 50, "筛选出列名有语|数字符")
print(data5)
